const vscode = require("vscode");
const fs = require("fs");
const path = require("path");

class NodeDependenciesProvider {
    constructor(isStack) {
        this.isStack = isStack;
        this._onDidChangeTreeData = new vscode.EventEmitter();
        this.onDidChangeTreeData = this._onDidChangeTreeData.event;
    }

    //  refresh()
    //  {
    //     this.valueList = undefined;
    //     // console.log(this.markObject.root);
    //     this._onDidChangeTreeData.fire();
    //  }
    refresh(value) {
        this.valueList = value;
        // console.log(this.markObject.root);
        this._onDidChangeTreeData.fire();
    }

    getTreeItem(element) {
        return element;
    }

    getChildren(element) {
        if (this.isStack && this.valueList !== undefined) {
            if (element === undefined) {
                let res = [];
                for (let i = 0; i < this.valueList.length; i++) {
                    res.push(new Dependency("[" + i + "]", null, null, i, vscode.TreeItemCollapsibleState.Expanded));
                }
                return Promise.resolve(res);
            } else {
                let res = [];
                // console.log("sun", this.valueList[element.index]);
                res.push(new Dependency(this.valueList[element.index][0], this.valueList[element.index][1], this.valueList[element.index][2], element.index, vscode.TreeItemCollapsibleState.None));
                return Promise.resolve(res);
            }
        } else if (this.isStack === false && this.valueList !== undefined) {
            if (element === undefined) {
                let res = [];
                res.push(new Dependency("Locals", null, null, 0, vscode.TreeItemCollapsibleState.Expanded));
                return Promise.resolve(res);
            } else {
                let res = [];
                if (element.key !== undefined) {
                    let children = this.valueList[element.key];
                    console.log(typeof children);
                    for (let ele in children) {
                        res.push(new Dependency(ele + " : " + children[ele], null, null, 0, vscode.TreeItemCollapsibleState.None));
                    }
                } else {
                    for (let key in this.valueList) {
                        let str = "";
                        if (typeof this.valueList[key] === "object") {
                            str = key + " : { . . . }";
                            res.push(new Dependency(str, null, null, 0, vscode.TreeItemCollapsibleState.Expanded, key));
                        } else {
                            str = key + " : " + this.valueList[key];
                            res.push(new Dependency(str, null, null, 0, vscode.TreeItemCollapsibleState.None));
                        }
                        let item = new DependencyItem(str, [[0, 0], [0, 5]]);
                        // res.push(new Dependency(str, null, null, 0, vscode.TreeItemCollapsibleState.None));
                    }
                }
                return Promise.resolve(res);
            }
        }
    }
}
class DependencyItem {
    constructor(label, range) {
        this.label = label;
        this.highlights = range;
    }
}
class DependencyAccessibilityInformation {
    constructor(label, role) {
        this.label = label;
        this.role = role;
    }
}


class Dependency extends vscode.TreeItem {
    pathExists(p) {
        try {
            fs.accessSync(p);
        } catch (err) {
            return false;
        }
        return true;
    }

    constructor(label, filename, line, index, collapsibleState, key = undefined) {
        super(label, collapsibleState);
        this.tooltip = `${this.label}`;
        this.key = key;
        this.description = filename;
        if (collapsibleState == vscode.TreeItemCollapsibleState.None) {
            this.contextValue = 'private';
        }
        this.filename = filename,
            this.index = index;
        this.line = line;
        let fullpath = vscode.workspace.workspaceFolders[0].uri.fsPath + "/" + filename;
        // if (this.pathExists(fullpath)) {
            this.command = {
                command: "TecoGDB.JumpToFile",
                arguments: [fullpath, line, index]
            }
        // }
    }
}
module.exports = { NodeDependenciesProvider };